#include"mainServer.h"
#include"Logger.h"
#include"mysqlDB.h"
#include"ipAddressPort.h"
#include<unistd.h>
#include<iostream>
MainServer::MainServer(){
 
}
MainServer::~MainServer(){


}
void  MainServer::ListenEventCallBack(int fd,short events,void * arg){
   MainServer * mainServer = static_cast<MainServer *>(arg);
   if(EV_READ * events){
   int cfd = mainServer->m_server->Accept();
   std::cout<<"connected"<<cfd<<std::endl;
   if(-1 == cfd){
     LOG_ERR( "Accept() failed!");
   }
   write(mainServer->m_Pool->GetSubThreadSocketPairFirst(),(char*)&cfd,4);
   }
}
void MainServer::start()
{
   m_server = new TcpServer(m_ipAddressPort);
   m_Pool = new ThreadPool(m_threadNum);
   m_reacotr = new Reactor();
   m_reacotr->AddEventAndHander(m_server->GetLfd(),EV_READ | EV_PERSIST,ListenEventCallBack,this);
   m_reacotr->Loop();
} 
void MainServer::SetThreadNum(int threadNum)
{
     m_threadNum = threadNum;
}
void MainServer::SetIpAddressPort(IpAddressPort ipAddressPort)
{
  m_ipAddressPort = ipAddressPort;
}
